<script setup lang="ts">
import {onMounted, ref, defineProps, defineEmits} from "vue"
import AMapLoader from '@amap/amap-jsapi-loader'

const props = defineProps(['isShow'])
const emits = defineEmits(['back','returnData'])

let map = null;
let searchInput = ref("")
// 存储标点位置
let positionArr = ref([113.392517,22.517024])
// 存储地址
let addressValue = ref("")
// 使用ref来存储Marker实例
let marker = ref(null)
let lngLatLng = ref(null)
let lngLatLat = ref(null)
let isShow = ref(props["isShow"])


const sureFn = ()=>{
  isShow.value = false
  emits('back',isShow.value)
  emits('returnData', {
    positionArr:positionArr.value,
    addressValue:addressValue.value
  })
}

const cancelFn = ()=>{
  isShow.value = false
  emits('back',isShow.value)
}

// 初始化配置地图
window._AMapSecurityConfig = {
  securityJsCode: "11fa46171b5a476ff40581ce7737421d",
};
AMapLoader.load({
  key: "7f8922a33480e520cc450d7ffa3efb8e", // 申请好的Web端开发者Key，首次调用 load 时必填
  version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
  plugins: ["AMap.PlaceSearch","AMap.AutoComplete","AMap.Marker"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
})
    // 异步加载插件
    .then((AMap) => {
      map = new AMap.Map("container", {
        // 设置地图容器id
        viewMode: "2D", // 地图模式
        zoom: 11, // 初始化地图级别
        center: [113.392517,22.517024], // 初始化地图中心点位置
      });

      // 地图点击事件
      map.on('click',(e)=>{
        console.log(e);
        lngLatLng.value = e.lnglat.lng;
        lngLatLat.value = e.lnglat.lat;
        console.log(lngLatLng.value, lngLatLat.value);
        positionArr.value = [lngLatLng.value, lngLatLat.value]
        addressValue.value = ""
        if (marker.value) {
          marker.value.setPosition([lngLatLng.value, lngLatLat.value]);  // 更新Marker位置
        }
      })

      let auto = new AMap.AutoComplete({
        city:"中山市", // 限定城市，默认全国
        input:"search_input",
      })
      let placeSearch = new AMap.PlaceSearch({
        city:"中山市",
        map:map
      });
      auto.on("select",(e)=>{
        placeSearch.search(e.poi.name)
        console.log(e.poi);
        positionArr.value = [e.poi.location.lng,e.poi.location.lat]
        addressValue.value = e.poi.address
        if (marker.value) {
          marker.value.setPosition([e.poi.location.lng,e.poi.location.lat]);  // 更新Marker位置
        }
      })
      // 标点位置
      marker.value = new AMap.Marker({
        position: [113.392517,22.517024], //经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
        map: map, // 当操作完成时，应该将结果展示在哪个地图上
      })
      map.add(marker)
    })
    .catch((e) => {
      console.log(e);
    });

</script>

<template>
  <div class="mask">
    <div id="box">
      <div class="title">
        选择位置
      </div>
      <div class="search">
        <el-input v-model="searchInput" placeholder="搜索" id="search_input"></el-input>
        <el-button type="primary">搜索</el-button>
      </div>
      <div id="container"></div>
      <div class="sure">
        <el-button type="primary" @click="sureFn">确认</el-button>
        <el-button @click="cancelFn">取消</el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.mask {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgba(0,0,0,.6);
}

#container {
  border-radius: 10px;
  padding:0;
  margin: 0;
  width: 100%;
  height: 100%;
}

.title {
  width: 100%;
  text-align: left;
  margin-bottom: 20px;
}

.search {
  width: 100%;
  height: 30px;
  display: flex;
  margin-bottom: 20px;
}

.search .el-input {
  width: 100%;
  height: 30px;
  border-radius: 5px;
}

.search :deep(.el-input__wrapper) {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.search .el-button {
  height: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-left: none;
}

#box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 50%;
  height: 80%;
  margin: 0 auto;
  background-color: #ffffff;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
  padding: 1.5vw;
}

.sure {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 20px;
  width: 100%;
}
</style>